/*

 * Created on 2008-9-18 

 *  

 * COPYRIGHT (C) 2006, ZyXEL Corporation (WuXi) Co., Ltd                  

 * Protected as an unpublished work. All Rights Reserved.

 * ZYXEL PROPRIETARY/CONFIDENTIAL.                                               

 * 

 * The computer program listings, specifications, and 

 * documentation herein are the property of ZYXEL 

 * Corporation and shall not be reproduced, copied, 

 * disclosed, or used in whole or in part for any reason 

 * without the prior express written permission of     

 * ZYXEL Corporation.                                    

 */


package cnm.validator.xpath.aaaserver;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import cnm.validator.xpath.XPathValidator;

/**
 * 
 * @author Lynn
 * @version
 *
 */
public class RadiusServerXPath extends XPathValidator {

	@Override
	public void xpathCheck() {
		if(logger.isDebugEnabled()) {
			logger.debug("Start XPath validate...");
		}
        XPathFactory xf = XPathFactory.newInstance();
          XPath path = xf.newXPath();
          XPathExpression exprOfType = null;
          Object resultOfType = null;
          NodeList nodes = null;
          try {
        	  Document doc = createDoc();
              exprOfType = path.compile("//DATASET/ATTRIBUTE[@name='_timeout']/text()");
              resultOfType = exprOfType.evaluate(doc, XPathConstants.NODESET);
              nodes = (NodeList) resultOfType;
              if(nodes.getLength() == 0) {
            	  if(logger.isDebugEnabled()) {
            		  logger.debug("[Validate fail], can't find '_timeout'");
            	  }
              } else {
	              for (int i = 0; i < nodes.getLength(); i++) {
	                  Integer value = Integer.valueOf(nodes.item(i).getNodeValue());
	                  if (value < 1 || value > 300) {
	                	  if(logger.isDebugEnabled()) {
	                		  logger.debug("[Validate fail]: " + value + " is not supported");
	                	  }
	                  }
	              }
              }
          } catch (XPathExpressionException e) {
              e.printStackTrace();
          }
          if(logger.isDebugEnabled()) {
        	  logger.debug("End XPath validate...");
          }
	}
}
